package com.processor;

import com.entity.TArticle;
import com.entity.TTag;
import com.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ArticleProcessor
 */
@Component
public class ArticleProcessor implements PageProcessor{

    private Logger LOG = LoggerFactory.getLogger(ArticleProcessor.class);

    private Site site = Site.me().setRetryTimes(10).setSleepTime(3000);

    public void process(Page page) {
        List<String> urls = page.getHtml().links().regex("http://blog.csdn.net/[^/]*/article/details/[0-9]*$").all();
        page.addTargetRequests(urls);
        String authorNick = page.getHtml().xpath("//div[@id='blog_userface']//a[@class='user_name']/text()").get();
        if (StringUtils.isBlank(authorNick)){
            authorNick = page.getHtml().xpath("//div[@id='skin_r']/div[@class='mess']/p[@class='mess_name']/text()").get();
        }
        String title = page.getHtml().xpath("//span[@class='link_title']/a/text()").get();
        if (StringUtils.isBlank(title)){
            title = page.getHtml().xpath("//h3[@class='list_c_t']//a/text()").get();
        }
        String aid = page.getUrl().regex("[0-9]*$").get();
        String readerAmount = page.getHtml().xpath("//span[@class='link_view']/text()")
                .regex("[0-9]+").get();
        if (StringUtils.isBlank(readerAmount)){
            readerAmount = page.getHtml().xpath("//*[@id='skin_center']/div[1]/div[1]/dl/dd/div[2]/p[2]/label[1]/span")
                    .regex("[0-9]+").get();
        }
        Integer commentAmount = new StatisProcessor().getCount(page.getUrl().get());
        String createTime = page.getHtml().xpath("//span[@class='link_postdate']/text()").get();
        if (StringUtils.isBlank(createTime)){
            String year = page.getHtml().xpath("//div[@class='date_t']/span/text()").get();
            String month = page.getHtml().xpath("//div[@class='date_t']/em/text()").get();
            String date = page.getHtml().xpath("//div[@class='date_b']/text()").get();
            createTime = composeTime(year,month,date);
        }
        String tagsStr = page.getHtml().xpath("//span[@class='link_categories']/tidyText()").replace("<.*?>","").get();
        TTag[] tags = new TTag[0];
        if (StringUtils.isBlank(tagsStr)){
            List<String> tagList = page.getHtml().xpath("//p[@class='detail_p']/label/em").all();
            tags = new TTag[tagList.size()];
            Pattern pattern = Pattern.compile(">.+<");
            for (int i = 0; i < tagList.size(); i++){
                TTag tTag = new TTag();
                tTag.setTitle(title);
                tTag.setAid(Integer.valueOf(aid));
                Matcher m = pattern.matcher(tagList.get(i));
                if (m.find()){
                    String tag = m.group(0);
                    tTag.setTag(tag.substring(1,tag.length()-1).toLowerCase());
                }
                else {
                    continue;
                }
                tTag.setUpdateTime(new Date());
                tags[i] = tTag;
            }

        }
        if (StringUtils.isNotBlank(aid)) {
            if (StringUtils.isNotBlank(tagsStr)) {
                tagsStr = tagsStr.substring(5, tagsStr.length() - 1).replace("\n", "");
                String[] tagStrArr = tagsStr.split(" ");
                tags = new TTag[tagStrArr.length];
                for (int i = 0; i < tagStrArr.length; i++) {
                    TTag tTag = new TTag();
                    tTag.setTitle(title);
                    tTag.setAid(Integer.valueOf(aid));
                    tTag.setTag(tagStrArr[i].toLowerCase());
                    tTag.setUpdateTime(new Date());
                    tags[i] = tTag;
                }
            }
            if (StringUtils.isNotBlank(aid)) {
                try {
                    TArticle tArticle = new TArticle();
                    tArticle.setAid(aid);
                    tArticle.setTitle(title);
                    tArticle.setAuthorNick(authorNick);
                    tArticle.setUrl(page.getUrl().get());
                    tArticle.setCommentAmount(commentAmount);
                    tArticle.setReadAmount(Integer.valueOf(readerAmount));
                    tArticle.setUpdateTime(new Date());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    tArticle.setCreateTime(simpleDateFormat.parse(createTime));
                    tArticle.setTags(tags);
                    page.putField("article", tArticle);
                } catch (ParseException e) {
                    LOG.error("error:{},url:{}",e.getMessage(),page.getUrl());
                } catch (Exception e) {
                    LOG.error("error:{},url:{}",e.getMessage(),page.getUrl());
                }
            }
        }
    }

    private String composeTime(String year, String month, String date) {
        return DateUtils.upperToLower(year + "-" + month + "-" + date + " 00:00");
    }

    public Site getSite() {
        return site;
    }
}
